package cn.biplam.back.bean.ddl.sql2db.fk;

import java.util.ArrayList;

import cn.biplam.back.sql.ddl.FK;
import cn.biplam.back.sql.ddl.Table;

public class FkCommen {
	
	public String get(Table table){
		ArrayList<FK> fks = table.getFKSet();
		
		if(fks.size()<1){
			return null;
		}
		/**初始化*/
		StringBuilder sb = new StringBuilder();
		for(FK mFK:fks){
			sb.append(" alter table "+mFK.getFkTableName()+"\n\t");
			sb.append(" add constraint "+mFK.getName()+" foreign key ("+mFK.getFkColumnNames()+")\n\t");
			sb.append(" references "+mFK.getPkTableName()+" ("+mFK.getPkColumnNames()+");\n");
		}
		return sb.toString();
	}
	
	/**
	 * 功能   得到外键删除语句
	 * @return
	 */
	public String getDrop(Table table){
		ArrayList<FK> fks = table.getFKSet();

		/**初始化*/
		StringBuilder sb = new StringBuilder();

		for(FK mFK:fks){
			sb.append(" alter table "+mFK.getFkTableName()+"\n\t");
			sb.append(" drop constraint "+mFK.getName()+";\n");
		}

		return sb.toString();
	}
}
